package com.tdk.bts.service;

import com.tdk.bts.constants.Constants;
import com.tdk.bts.dao.BookInfoServiceDao;
import com.tdk.bts.entity.BookInfo;
import com.tdk.bts.service.BookInfoService;
import com.tdk.bts.util.Page;
import com.tdk.bts.util.ValidCodeUtil;
import com.tdk.bts.vo.BookInfoVO;
import com.tdk.bts.vo.BookVO;

import java.sql.SQLException;
import java.util.List;

public class BookInfoServiceImp implements BookInfoService {
    BookInfoServiceDao bookInfoServiceDao = new BookInfoServiceDao();
    @Override
    public String getValidCode(int screenId, String[] seats, String phone) throws SQLException {
      String validC = ValidCodeUtil.Shengcheng();
      bookInfoServiceDao.insertValidCodes(screenId, seats, phone,validC);
        return validC;
    }

    @Override
    public BookInfoVO getTicket(String phone, String validCode) throws SQLException {
        BookInfoVO ticket = bookInfoServiceDao.getTicketInfo(phone, validCode);
        if (ticket != null) {
            bookInfoServiceDao.receiveTicket(phone, validCode);
        }
        return ticket;
    }

    @Override
    public Page<BookVO> findBookVOByPage(int pageNo) throws SQLException {
        int count = bookInfoServiceDao.BookInfoCount();
        List<BookVO> listBookVO = bookInfoServiceDao.findBookInfo(pageNo);
        Page<BookVO> bookVOS = new Page<>(pageNo, Constants.PAGE_SIZE,listBookVO,count);
        return bookVOS;
    }

    @Override
    public List<BookInfo> findBookInfoByScreenId(int id) throws SQLException {
        List<BookInfo> list = bookInfoServiceDao.findBookInfoByScreenIds(id);
        return list;
    }
}
